<?php


namespace app\api\controller;


use think\Controller;
use think\Request;

class Questionnaire extends Controller
{
    public function addQuestion(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['pid']) or !isset($data['type'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $insertData['type'] = $data['type'];
        $insertData['pid'] = $data['pid'];
        $insertData['user_id'] = $data['user_id'];
        unset($data['type']);unset($data['pid']);unset($data['user_id']);
        $insertData['question'] = json_encode($data);
        $insertData['create_time'] = time();

        $questionnaire = new \app\admin\model\Questionnaire();
        //防止重复提交
        $where['user_id'] = $insertData['user_id'];
        $lastData = $questionnaire->getLast($where);
        if(time()-strtotime($lastData['create_time']) >= 300){
            $rs = $questionnaire->add($insertData);
            if($rs){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '提交成功';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '提交失败';
            }
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '5分钟禁止重复提交';
        }

        $res['status'] = 200;
        echo json_encode($res);
    }
}